package com.brandongogetap.stickyheaders.demo;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerAbilitySlice extends AbilitySlice {
    private List<Component> mPageViewList;
    private TabList mTabList;

    private final static int DEFAULE_INDEX = 0;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_view_pager);
        Image arrowImage = (Image) findComponentById(ResourceTable.Id_arrow_image);
        arrowImage.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                jumpAbility();
            }
        });

        mTabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        mTabList.setTabLength(100); // 设置Tab的宽度
        TabList.Tab tab1 = mTabList.new Tab(getContext());
        tab1.setText("1");
        mTabList.addTab(tab1);
        TabList.Tab tab2 = mTabList.new Tab(getContext());
        tab2.setText("2");
        mTabList.addTab(tab2);
        TabList.Tab tab3 = mTabList.new Tab(getContext());
        tab3.setText("3");
        mTabList.addTab(tab3);
        TabList.Tab tab4 = mTabList.new Tab(getContext());
        tab4.setText("4");
        mTabList.addTab(tab4);
        TabList.Tab tab5 = mTabList.new Tab(getContext());
        tab5.setText("5");
        mTabList.addTab(tab5);

        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager_slider);
        LayoutScatter layoutScatter = LayoutScatter.getInstance(getContext());
        DependentLayout dependentLayout1 = (DependentLayout) layoutScatter.parse(ResourceTable.Layout_pageSlider1, null, false);
        DependentLayout dependentLayout2 = (DependentLayout) layoutScatter.parse(ResourceTable.Layout_pageSlider2, null, false);
        DependentLayout dependentLayout3 = (DependentLayout) layoutScatter.parse(ResourceTable.Layout_pageSlider3, null, false);
        DependentLayout dependentLayout4 = (DependentLayout) layoutScatter.parse(ResourceTable.Layout_pageSlider4, null, false);
        DependentLayout dependentLayout5 = (DependentLayout) layoutScatter.parse(ResourceTable.Layout_pageSlider5, null, false);
        //将view装入数组
        mPageViewList = new ArrayList<>();
        mPageViewList.add(dependentLayout1);
        mPageViewList.add(dependentLayout2);
        mPageViewList.add(dependentLayout3);
        mPageViewList.add(dependentLayout4);
        mPageViewList.add(dependentLayout5);
        //绑定适配器
        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            //获取当前窗体界面数
            public int getCount() {
                return mPageViewList.size();
            }

            //返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的mPageViewList中
            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                componentContainer.addComponent(mPageViewList.get(i));
                return mPageViewList.get(i);
            }

            //是从ViewGroup中移出当前View
            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                componentContainer.removeComponent(mPageViewList.get(i));
            }

            //断是否由对象生成界面
            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                return component == o;
            }
        });

        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
                //v:指示页面的位置偏移量。值的范围是(0,1]。
                // 0表示正在显示目标页面。
                //i1:指示显示页面的位置偏移像素数。
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
                mTabList.selectTab(mTabList.getTabAt(i));
            }
        });

        mTabList.addTabSelectedListener(new TabSelectListener(this, pageSlider));
        // 设置默认选择的页签项
        mTabList.getTabAt(DEFAULE_INDEX).select();
    }


    public static class TabSelectListener implements TabList.TabSelectedListener {

        private AbilitySlice slice;

        private PageSlider pageSlider;

        public TabSelectListener(AbilitySlice slice, PageSlider pageSlider) {
            this.slice = slice;
            this.pageSlider = pageSlider;
        }

        public AbilitySlice getSlice() {
            return slice;
        }

        @Override
        public void onSelected(TabList.Tab tab) {
            if (tab == null) {
                return;
            }
            if (pageSlider != null) {
                pageSlider.setCurrentPage(tab.getPosition());
            }
        }

        @Override
        public void onUnselected(TabList.Tab tab) {

        }

        @Override
        public void onReselected(TabList.Tab tab) {

        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void jumpAbility() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.brandongogetap.stickyheaders.demo")
                .withAbilityName("com.brandongogetap.stickyheaders.demo.MainAbility")
                .build();
        intent.setOperation(operation);
        startAbility(intent);
        terminateAbility();
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        jumpAbility();
    }
}